<?php

#doc
#	classname:	CustomerController
#	scope:		PUBLIC
#
#/doc

class CustomerController extends Controller
{
	#	internal variables
	var $private = true;
	#	Constructor
	function __construct (  )
	{
		$this->useModel('Customer');
		Classloader::getInstance()->loadPlugin('Menu');
		Menu::getInstance()->addMenuEntry("Add", Utility::buildUrl("Customer", "create"));
		Menu::getInstance()->addMenuEntry("List All", Utility::buildUrl("Customer", "listAll"));
	}

	public function show ()
	{
		$id = $_GET['id'];
		$this->customer->find($id);
	}

	public function listAll ( )
	{
		$this->customers = $this->customer->findAll();
	}

	public function getAllAsJson ()
	{
		$this->customers = $this->customer->findAll();
		$this->renderAsJson($this->customers);
	}

	public function create ()
	{
		if(isset($_POST['name'])){
			$this->customer->name = $_POST['name'];
			$this->customer->address = $_POST['address'];
			$this->customer->save();
			RoutingEngine::getInstance()->redirectToAction('customer','show',$this->customer->id);
			$this->flash('New customer created');
		}
	}

	public function delete ()
	{
		$this->customer->find($_GET['id']);
		$this->customer->delete();
		RoutingEngine::getInstance()->redirectToAction('customer','listAll');
	}

	public function edit ()
	{
		$this->customer->find($_GET['id']);
		if($_POST['action']=='update'){
			$this->customer->name = $_POST['name'];
			$this->customer->address = $_POST['address'];
			$this->customer->save();
			RoutingEngine::getInstance()->redirectToAction('customer','show',$this->customer->id);
		}

	}

}

?>